#pragma once

#include "Callback.h"

using namespace AT;

class CUIThreadTask
{
public:
	CUIThreadTask();
	~CUIThreadTask();

	bool Init();
	void Exit();
	bool PostTask(ICallbackPtr&& pTask);
	bool PostTask(UINT nDelayMs, ICallbackPtr&& pTask);

private:
	LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
	void RunTask();

	static CUIThreadTask *m_pThis;

	HWND    m_hWnd;
	WNDPROC m_pSuperProc;
	bool    m_bInRunTask;
	SRWLOCK m_SRWLock = SRWLOCK_INIT;
	std::deque<ICallbackPtr> m_deqTasks;

	struct TimedTask
	{
		UINT nDueTime;
		ICallbackPtr pTask;

		bool operator<(const TimedTask& task) const
		{
			if (nDueTime != task.nDueTime)
				return nDueTime > task.nDueTime;
			else
				return pTask->GetId() > task.pTask->GetId();
		}
	};

	UINT m_nTaskDueTime;	// Due time of the first task
	std::priority_queue<TimedTask> m_queTimedTasks;
};
